<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-item">
        <span class="span">任务名称 </span>
        <el-input v-model="listQuery.jobName" placeholder="请输入任务名称" style="width: 200px" />
      </div>
      <!-- <div class="filter-item">
        <span>核查数据源名称:</span>
        <el-input v-model="listQuery.sourceName" placeholder="请选择核查数据源名称" style="width: 200px" />

      </div>
      <div class="filter-item">
        <span>核查表名称:</span>

        <el-input v-model="listQuery.tableName" placeholder="请选择核查表名称" style="width: 200px" />

      </div>
      <div class="filter-item">
        <span>核查字段名称:</span>

        <el-input v-model="listQuery.columnName" placeholder="请选择核查字段名称" style="width: 200px" />

      </div> -->
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        {{ $t('table.search') }}
      </el-button>
      <el-button v-waves plain v-permission="['job:metadataJob:add']" class="filter-item" style="margin-left: 10px" type="primary" icon="el-icon-edit" @click="handleCreate">
        {{ $t('table.add') }}
      </el-button>
      <!--      <el-button  v-permission="['task:schedule:job:refresh:job']" class="filter-item" type="primary" icon="el-icon-refresh" @click="handleRefresh">刷新</el-button>-->
      <el-button v-waves class="filter-item" icon="el-icon-refresh" @click="handleRest">{{ $t('table.rest') }}</el-button>
    </div>

    <el-table ref="table" :key="tableKey" v-loading="listLoading" :data="list" element-loading-text="正在加载，请稍后" border fit highlight-current-row style="width: 100%">
      <el-table-column type="index" label="序号" width="50" align="center" />
      <el-table-column label="任务名称" align="center" min-width="150px">
        <template slot-scope="{ row }">
          <span>{{ row.jobName }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="核查数据源名称" min-width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.sourceName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="核查表名称" min-width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.tableName }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="核查字段名称" min-width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.columnName }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="任务状态" min-width="150px" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.checkStatus | dictLabel('is_use') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="核查时间" prop="checkDate" :formatter="formatDate" align="center" min-width="150px">
        <!-- <template slot-scope="{row}">
          <span>{{ row.checkDate}}</span>
        </template> -->
      </el-table-column>
      <el-table-column label="cron表达式" align="center" min-width="150px">
        <template slot-scope="{ row }">
          <span>{{ row.cronExpression }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="规则名称" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.rulesName }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="操作" align="center" min-width="150" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button v-permission="['job:metadataJob:update']" type="text" size="small" icon="el-icon-edit" @click="handleUpdate(row)">
            {{ $t('table.edit') }}
          </el-button>
          <el-button v-permission="['job:metadataJob:delete']" type="text" size="small" icon="el-icon-delete" class="delete-text-btn" @click="handleDelete(row,'deleted')">
            {{ $t('table.delete') }}
          </el-button>
        </template>
      </el-table-column> -->
      <el-table-column fixed="right" :label="$t('table.actions')" align="center" class-name="small-padding fixed-width " min-width="200">
        <template #default="scope">
          <el-button
            v-if="scope.row.checkStatus == 0"
            v-permission="['task:schedule:job:change:job:status']"
            type="text"
            size="small"
            icon="el-icon-video-play"
            style="margin-left: 10px"
            @click="handleChangeJobStatus(scope.row, '1', '启动')"
            >开始</el-button
          >
          <el-button
            v-if="scope.row.checkStatus == 1"
            v-permission="['task:schedule:job:change:job:status']"
            type="text"
            size="small"
            icon="el-icon-video-pause"
            class="delete-text-btn"
            style="margin-left: 10px"
            @click="handleChangeJobStatus(scope.row, '0', '停止')"
            >停止</el-button
          >

          <el-button v-permission="['task:schedule:job:change:job:status']" size="small" type="text" icon="el-icon-video-play" @click="runAJobNow(scope.row)">执行一次</el-button>
          <!-- <el-button v-permission="['task:schedule:job:detail']" size="small" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">{{ $t('table.edit') }}</el-button> -->
          <el-button v-permission="['job:metadataJob:update']" type="text" size="small" icon="el-icon-edit" @click="handleUpdate(scope.row)">
            {{ $t('table.edit') }}
          </el-button>
          <el-button icon="el-icon-document" size="small" type="text" @click="handleLook(scope.row)"> {{ $t('table.look') }}</el-button>
          <el-button v-permission="['task:schedule:job:delete']" size="small" type="text" icon="el-icon-delete" class="delete-text-btn" @click="handleDelete(scope.row)">{{
            $t('table.delete')
          }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" :page-sizes="pageArray" @pagination="getList" />

    <metadata-job-form ref="form" @refreshList="getList" />
  </div>
</template>

<script>
import { deletemetadataJob, fetchmetadataJobList, changeJobStatus, refresh, startOne } from '@/api/job/metadataJob'
import waves from '@/directive/waves' // waves directive
import permission from '@/directive/permission/permission'
import metadataJobForm from './metadataJobForm'
import Pagination from '@/components/Pagination'
import dayjs from 'dayjs'
export default {
  name: 'metadataJobList',
  components: { metadataJobForm, Pagination },
  directives: { waves, permission },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      pageArray: this.$store.getters.pageArray,
      listQuery: {
        jobName: undefined,
        sourceName: undefined,
        tableName: undefined,
        columnName: undefined,
        // page: 1,
        // pageNum: 1,
        // pageSize: 10,
        limit: this.$store.getters.defaultPageSize,
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    formatDate(row, column) {
      // 获取单元格数据
      let data = row[column.property]
      if (data == null) {
        return null
      }
      let dt = new Date(data)
      return dayjs(dt).format('YYYY-MM-DD HH:DD:ss')
    },
    getList() {
      this.listLoading = true
      fetchmetadataJobList(this.listQuery).then((response) => {
        if (response.data.code == 200) {
          this.list = response.data.data.rows
          this.total = response.data.data.total
          this.listLoading = false
        } else {
          this.$message.error(response.data.msg)
        }
      })
    },
    handleCreate() {
      this.$refs.form.handleCreate()
    },
    handleUpdate(row) {
      this.$refs.form.handleUpdate(row.id)
    },
    handleLook(row) {
      this.$refs.form.handleLook(row.id)
    },
    handleDelete(row) {
      this.$confirm('确定删除该数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        deletemetadataJob(row.id).then((response) => {
          if (response.data.code === 0) {
            this.getList()
            this.$message.success(response.data.msg)
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    },
    handleFilter() {
      // this.listQuery.pageNum = 1
      this.getList()
    },
    //重置
    handleRest() {
      this.listQuery = {
        page: 1,
        limit: this.$store.getters.defaultPageSize,
      }
      this.getList()
    },
    runAJobNow(row) {
      startOne({ id: row.id }).then((res) => {
        if (res.data.code == 0) {
          this.$message.success(res.data.msg)
          this.getList()
        } else {
          this.$message.error(res.data.msg)
        }
      })
    },
    //刷新
    handleRefresh() {
      refresh().then((res) => {
        if (res.data.code == 0) {
          this.$message.success(res.data.msg)
        } else {
          this.$message.error(res.data.msg)
          this.getList()
        }
      })
    },
    //开始终止
    handleChangeJobStatus(row, cmd, cmdLabel) {
      this.$confirm('你确定要' + cmdLabel + '该计划任务么?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        let obj = {
          jobName: row.jobName,
          id: row.id,
          checkStatus: cmd,
        }
        changeJobStatus(obj).then((response) => {
          const data = response.data
          if (data.code === 0) {
            this.$message.success(data.msg)
            this.getList()
          } else {
            this.$message.error(data.errmsg)
          }
        })
      })
    },
  },
}
</script>
<style lang="scss" scoped>
::v-deep(::-webkit-scrollbar) {
  display: block !important;
}
.table-expand {
  font-size: 0;
}
.table-expand label {
  width: 90px;
  color: #99a9bf;
}
.table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
.div {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}
.span {
  color: #606266;
  font-weight: 700;
  font-size: 14px;
}
</style>
